#!/bin/bash

Usage="Usage: $0 source_directory servername [fc|bj]"
SRC_DIR="$1" && [ ! -d "$SRC_DIR" ] && echo $Usage && exit 1
DST_SVR="$2" && [ -z "$DST_SVR" ] && echo $Usage && exit 2
DST_LOC="$3" && [ -z "$DST_LOC" ] && DST_LOC="bj"
[ "$DST_LOC" != "fc" -a "$DST_LOC" != "bj" ] && exit 3
ZIP_NAME=`echo $SRC_DIR | awk -F '/' '{for (i=1;i<NF;i++) if (length($i) > 0) printf("%s.", $i); printf("\n")}'`@`hostname`.tgz
echo $ZIP_NAME
PRV_DIR=lance

function _locate()
{
    INC_DIR=`dirname $0`"/../"
    [ -d "$INC_DIR" ] && find "$INC_DIR" -name "$1" | head -n 1
}

SCP_CMD="scp.exp"
SCR_CMD="scr.exp"

SCP=`_locate $SCP_CMD` && [ ! -f "$SCP" ] && echo "Can not locate scp.exp, quit!" && exit 1
SCR=`_locate $SCR_CMD` && [ ! -f "$SCR" ] && echo "Can not locate scr.exp, quit!" && exit 1

PROXY_SVR=ldl.fc.hp.com
PROXY_USR=lance
PROXY_PWD=zdhs16
GEN_USR=root
GEN_PWD=iforgot

# Setup Proxy
if [ "$DST_LOC" == "fc" ]
then
    $SCP $PROXY_PWD "$SCP" $PROXY_USR@$PROXY_SVR:~
    $SCP $PROXY_PWD "$SCR" $PROXY_USR@$PROXY_SVR:~
fi

# Compress file
tar -czf ./$ZIP_NAME $SRC_DIR
$SCR $GEN_PWD $GEN_USR@$DST_SVR ~ "mkdir -p $PRV_DIR"

# Transfer file
$SCP $GEN_PWD ./$ZIP_NAME $GEN_USR@$DST_SVR:~/$PRV_DIR/

# Expand file on target
$SCR $GEN_PWD $GEN_USR@$DST_SVR ~/$PRV_DIR "tar -xzf ./$ZIP_NAME ; rm -f ./$ZIP_NAME"

# Clean Proxy
if [ "$DST_LOC" == "fc" ]
then
    $SCR $PROXY_PWD $PROXY_USR@$PROXY_SVR ~ "rm -f $SCR_CMD $SCP_CMD $ZIP_NAME "
fi
rm -rf ./$ZIP_NAME

exit 0
export http_proxy=http://web-proxy.cce.hp.com:8088
export http_proxy=http://web-proxy.cce.hp.com:8088
export ftp_proxy=http://web-proxy.cce.hp.com:8088
export no_proxy='localhost, 127.0.0.1, 10.*, *.test, *.mgmt'
export HTTP_PROXY=http://web-proxy.cce.hp.com:8088

wget http://linuxqa.chn.hp.com/pbfd/share/lance/docs/Lance_class_b.p12
wget http://linuxqa.chn.hp.com/pbfd/share/lance/docs/servers

mv ./Lance_class_b.p12 /root/.subversion/
mv ./servers /root/.subversion/

mkdir ./ezbmc
mkdir ./ezpci
mkdir ./hpmgmtbase
mkdir ./smx

svn co https://oslo-ezbmc.csd200a.com/svn/oslo-ezbmc/trunk ./ezbmc
svn co https://oslo-ezpci.csd200a.com/svn/oslo-ezpci/trunk ./ezpci
svn co https://oslo-hpmgmtbase.csd200a.com/svn/oslo-hpmgmtbase/trunk ./hpmgmtbase
svn co https://oslo-smx.csd200a.com/svn/oslo-smx/trunk ./smx


